package xnj.lazydog.btcontroller.MyViews;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class PercentView extends View {

    Paint p;
    float MIN_FLOAT = 0.0000001f;

    public int [] colors = new int[]{0xff231815,0xff6666dd,0xff33aadd,0xff55aa66,0xffeeaa66,0xffff5555,0xff888888};

    float [] prop;
    float [] prop_d;
    float [] values;
    RectF [] rects;
    boolean dataChanged = false;
    boolean isDrawing = true;

    public PercentView(Context context,AttributeSet attrs) {
        super(context, attrs);
        p = new Paint();
        p.setAntiAlias(true);
    }

    float rect_edge_ratio = 0.01f;
    float shrink_ratio;

    public void setData(int [] values)
    {
        float sum=0;
        prop= new float[values.length];
        this.values = new float[values.length];

        int none_zero_count = 0;

        for(int i=0;i<values.length;i++)
        {
         prop[i] = values[i];
         this.values[i] = values[i];
         sum+=values[i];
         if(values[i]!=0)
             none_zero_count++;
        }

        shrink_ratio = 1-((none_zero_count)*rect_edge_ratio);


        if(sum==0)
        {
            for(int i=0;i<values.length;i++)
                prop[i] =0;
        }else {
            for (int i = 0; i < values.length; i++)
                prop[i] = prop[i]*shrink_ratio / sum;
        }
        if(prop_d==null||prop_d.length!=prop.length)
            prop_d = prop.clone();
        dataChanged = true;
        if(!isDrawing)
            postInvalidate();
        isDrawing = true;

    }
    public void setData(float [] values)
    {
        float sum=0;
        prop= new float[values.length];
        this.values = new float[values.length];
        int none_zero_count = 0;
        for(int i=0;i<values.length;i++)
        {
            prop[i] = values[i];
            this.values[i] = values[i];
            sum+=values[i];
            if(Math.abs(values[i])>MIN_FLOAT)
                none_zero_count++;
        }

        shrink_ratio = 1-none_zero_count*rect_edge_ratio;

        if(sum==0)
        {
            for(int i=0;i<values.length;i++)
                prop[i] =0;
        }else {
            for (int i = 0; i < values.length; i++)
                prop[i] = prop[i] *shrink_ratio/ sum;
        }
        if(prop_d==null||prop_d.length!=prop.length)
            prop_d = prop.clone();
        dataChanged = true;

        if(!isDrawing)
            postInvalidate();
        isDrawing = true;
    }

    float rect_edge_px;
    void updateRect()
    {
        float start_x = w*(1-widthRatio)*0.5f;
        float center_y = h*0.5f;
        float half_h = w*widthRatio*heightWidthRatio*0.5f;
        rect_edge_px = w*rect_edge_ratio;
        float last_x = start_x;
        float delta_x = w*widthRatio;

        if(prop_d==null)
            return;
        if(rects==null||rects.length!=prop_d.length) {
            rects = new RectF[prop_d.length];
        }
            for (int i = 0; i < prop_d.length; i++) {
                rects[i] = new RectF(last_x, center_y - half_h, last_x + prop_d[i] * delta_x, center_y + half_h);
                if(Math.abs(prop_d[i])>MIN_FLOAT)
                    last_x = rects[i].right + rect_edge_px;

        }
    }

    public void setColors(int [] colors)
    {
        this.colors = colors;
    }

    //// 视图宽高
    float w,h;

    float widthRatio = 0.92f;
    float heightWidthRatio = 0.06f;

    float sum;
    float delta;
    private static final String TAG = "PercentView";
    @Override
    protected void onDraw(Canvas canvas) {

        w = getWidth();
        h = getHeight();

        if(prop!=null) {
            sum = 0;
            for (int i = 0; i < prop.length; i++) {
                delta = prop[i] - prop_d[i];
                sum+=Math.abs(delta);
                prop_d[i]+=delta*0.2f;
            }

            if(sum<0.006f) {
                isDrawing = false;
                for (int i = 0; i < prop.length; i++) {
                    prop_d[i]=prop[i];
                }
            }
        }

        updateRect();
        if(rects!=null) {
            for (int i = 0; i < rects.length; i++) {
                p.setColor(colors[i]);
                canvas.drawRoundRect(rects[i],rect_edge_px,rect_edge_px,p);
            }
        }
        if(isDrawing)
            invalidate();
    }
}
